package com.jsb.laundry.model.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jsb.laundry.model.entity.JsbPosts;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 帖子视图
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "帖子信息")
public class PostsVO implements Serializable {

    private final static Gson GSON = new Gson();

    @ApiModelProperty(value = "帖子id", dataType = "int")
    private Integer id;

    @ApiModelProperty(value = "帖子标题", dataType = "string")
    private String title;

    @ApiModelProperty(value = "帖子内容", dataType = "string")
    private String content;

    @ApiModelProperty(value = "图片列表 json", dataType = "List<String>")
    private List<String> imgList;

    @ApiModelProperty(value = "帖子点赞数", dataType = "int")
    private Integer likes;

    @ApiModelProperty(value = "帖子收藏数", dataType = "int")
    private Integer favorites;

    @ApiModelProperty(value = "评论数", dataType = "int")
    private Integer comments;

    @ApiModelProperty(value = "帖子创建的用户id", dataType = "string")
    private String userId;


    @ApiModelProperty(value = "创建时间", dataType = "date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdAt;

    @ApiModelProperty(value = "更新时间", dataType = "date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedAt;

    @ApiModelProperty(value = "标签列表", dataType = "List<String>")
    private List<String> tagList;

    @ApiModelProperty(value = "发帖人信息", dataType = "user")
    private UserVO user;

    @ApiModelProperty(value = "是否已过赞", dataType = "Boolean")
    private Boolean hasLike;

    @ApiModelProperty(value = "是否已收藏", dataType = "Boolean")
    private Boolean hasFavour;

    @ApiModelProperty(value = "是否关注", dataType = "Boolean")
    private Boolean hasFollow;

    /**
     * 包装类转对象
     *
     * @param postsVO
     * @return
     */
    public static JsbPosts voToObj(PostsVO postsVO) {
        if (postsVO == null) {
            return null;
        }
        JsbPosts post = new JsbPosts();
        BeanUtils.copyProperties(postsVO, post);
        List<String> tagList = postsVO.getTagList();
        if (tagList != null) {
            post.setTags(GSON.toJson(tagList));
        }
        return post;
    }

    /**
     * 对象转包装类
     *
     * @param posts
     * @return
     */
    public static PostsVO objToVo(JsbPosts posts) {
        if (posts == null) {
            return null;
        }
        PostsVO postVO = new PostsVO();
        BeanUtils.copyProperties(posts, postVO);
        postVO.setImgList(GSON.fromJson(posts.getImages(), new TypeToken<List<String>>() {}.getType()));
        postVO.setTagList(GSON.fromJson(posts.getTags(), new TypeToken<List<String>>() {}.getType()));
        return postVO;
    }
}
